//
//  RecommendVC.m
//  echo
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 Danny. All rights reserved.
//

#import "RecommendVC.h"
#import <SDWebImage/UIButton+WebCache.h>
#import "NetworkingHandler.h"
#import "ModelForRecommendScrollView.h"
#import "CellForRecommendTableView.h"
#import "ModelForRecommendTableViewCell.h"
#import "MusicPlayerVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SearchVC.h"
#import <DKNightVersion.h>
#import "MyExploreVC.h"

@interface RecommendVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)UIButton *buttonForTop;
@property (nonatomic, retain)NSMutableArray *tempArr;
@property (nonatomic, retain)NSMutableArray *arrForCell;
@property (nonatomic, retain)UIButton *buttonForSearch;
@end

@implementation RecommendVC

- (void)dealloc{
    [_headerView release];
    [_buttonForTop release];
    [_tempArr release];
    [_arrForCell release];
    [_buttonForSearch release];
    self.tableView.delegate = nil;
    [_tableView release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postModelForLeftOfRecommend" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"postModelForRightOfRecommend" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"NoNet" object:nil];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    [self handleDataForScrollViewImage];
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForLeftOfRecommend" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForRecommendTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelForRecommendTableViewCell:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"postModelForRightOfRecommend" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
        ModelForRecommendTableViewCell *model = [note.userInfo objectForKey:@"model"];
        [vc passModelForRecommendTableViewCell:model];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }];
    


}

- (void)handleDataForScrollViewImage{
    
    [NetworkingHandler networkingGetWithUrl:@"http://echosystem.kibey.com/index/banner" dicForValue:@{@"User-Agent":@"echo ios 1.0 20151114;(iPhone,iPhone OS8.3);IDFA F37139C2-8"} completeHandle:^(id result) {
        
        NSArray *arr = [result objectForKey:@"result"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ModelForRecommendScrollView *model = [[ModelForRecommendScrollView alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            [model release];
        }
        
        [self.arr addObject:[self.arr firstObject]];
        [self.arr insertObject:[self.arr objectAtIndex:self.arr.count - 2] atIndex:0];
        [self handleDataForTableViewCell];
        
    }];
}

- (void)handleDataForTableViewCell{
    [NetworkingHandler networkingGetWithUrl:@"http://echosystem.kibey.com/sound/hot?page=1" dicForValue:@{@"x-v":@"20160209", @"x-c":@"3"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *arr = [dic objectForKey:@"data"];
        self.tempArr = [NSMutableArray array];
        
        for (NSDictionary *dic2 in arr) {
            ModelForRecommendTableViewCell *model = [[ModelForRecommendTableViewCell alloc]init];
            NSDictionary *tempDic2 = [dic2 objectForKey:@"sound"];
            [model setValuesForKeysWithDictionary:tempDic2];
            NSDictionary *tempDic = [tempDic2 objectForKey:@"channel"];
            model.channel = [tempDic objectForKey:@"name"];
            
            [self.tempArr addObject:model];
            
            [model release];
            
        }
        
        self.arrForCell = [NSMutableArray array];
        
        for (int i = 0; i < self.tempArr.count; i++) {
            if (i%2 == 0) {
                NSMutableArray *tempArr2 = [NSMutableArray array];
                [tempArr2 addObject:self.tempArr[i]];
                [tempArr2 addObject:self.tempArr[i+1]];
                [self.arrForCell addObject:tempArr2];
            }
        }
        [self createScrollView];
    }];
}

- (void)createScrollView{
#pragma mark ** 2个非常重要的时间点!!
    [super createScrollView];
    [self createTableView];
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 76) style:UITableViewStyleGrouped];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 不确定 headerView release!!!
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 275 * HEIGHT)];
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.buttonForTop = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForTop.frame = CGRectMake(0, 0, self.view.frame.size.width, 75 * HEIGHT);
    [self.headerView addSubview:self.buttonForTop];
    [self.buttonForTop sd_setBackgroundImageWithURL:[NSURL URLWithString:@"http://echo-image.qiniucdn.com/FvBAwZEKzRM8Twly11NskLFJYSsn?imageView2/4/w/640"] forState:UIControlStateNormal];
    [self.buttonForTop addTarget:self action:@selector(handleMusic) forControlEvents:UIControlEventTouchUpInside];
    
    [self.headerView addSubview:self.backView];
    self.backView.frame = CGRectMake(0, 75 * HEIGHT, self.view.frame.size.width, 200 * HEIGHT);
    [self.tableView registerClass:[CellForRecommendTableView class] forCellReuseIdentifier:@"poolForCellOfRecommend"];

    self.buttonForSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.navigationController.navigationBar addSubview:self.buttonForSearch];
    self.buttonForSearch.frame = CGRectMake(15 * WIDTH, 2.5 * HEIGHT, 35 * WIDTH, 35 * WIDTH);
    [self.buttonForSearch setBackgroundImage:[UIImage imageNamed:@"iconfont-search"] forState:UIControlStateNormal];
    [self.buttonForSearch addTarget:self action:@selector(handleSearch) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleMusic {
    MusicPlayerVC *vc = [[MusicPlayerVC alloc]init];
    [vc passModelModelForHairVC];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)handleSearch{
    SearchVC *searchVC = [[SearchVC alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
//    [searchVC release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrForCell.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellForRecommendTableView *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCellOfRecommend"];
    NSArray *arr = [self.arrForCell objectAtIndex:indexPath.row];
    [cell passModelForCell:arr];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    // 没release!!!
    UIView *viewOfBackForHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45 * HEIGHT)];
    viewOfBackForHeader.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    UILabel *labelForHeader = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 3 - 5 * WIDTH, 12 * HEIGHT, self.view.frame.size.width / 3, 30 * HEIGHT)];
    [viewOfBackForHeader addSubview:labelForHeader];
    [labelForHeader release];
    
    labelForHeader.layer.cornerRadius = 15 * WIDTH;
    labelForHeader.layer.masksToBounds = YES;
    labelForHeader.font = [UIFont fontWithName:@"ArialUnicodeMS" size:17.0];
    labelForHeader.text = @"echo每日推荐";
    labelForHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor orangeColor]);
    labelForHeader.textAlignment = NSTextAlignmentCenter;
    labelForHeader.backgroundColor = [UIColor colorWithRed:146 / 255.f green:255 / 255.f blue:147 / 255.f alpha:1];

    return viewOfBackForHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 45 * HEIGHT;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 257 * HEIGHT;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:13 / 256.f green:19 / 256.f blue:30 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:18 / 256.f green:29 / 256.f blue:50 / 256.f alpha:1]);

    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
